package com.ruoyi.web.controller.wx;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.domain.ByBsSubject;
import com.ruoyi.system.domain.BySubjectClassify;
import com.ruoyi.system.service.IByBsService;
import com.ruoyi.system.service.IBySubjectClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/subject")
@Validated
public class WxSubjectController {

    @Autowired
    private IBySubjectClassifyService subjectClassifyService;


    @Autowired
    private IByBsService byBsService;


    @PostMapping("loadJcData")
    public AjaxResult loadJcData(@RequestBody String param){
        String userid = JSONObject.parseObject(param).getString("userid");
        ByBs bsEntity = byBsService.queryBsEntityByWxid(userid);
//        System.out.println(bsEntity.getString("id"));
        if(bsEntity == null){ // 获取帮手信息 未获取到数据，不能进行设置版型
            // bsEntity = new JSONObject();
            return AjaxResult.error(400,"你还不是帮手，请先进行帮手注册！");
        }
        // 获取全部的开设科目数据
//        List<BySubjectClassify> obj = subjectClassifyService.queryAllTable();
        // 获取帮手的设置科目的数据
        Long bsid = bsEntity.getId();
//        List<ByBsSubject> bsSubjectList = byBsService.getSubjectIdForByid(bsid.toString());

        List<BySubjectClassify> obj = subjectClassifyService.queryAllTableForBs(bsid.toString());


        /*Map map = new HashMap();
        map.put("allSubject",obj);
        map.put("bsAllSubject",bsSubjectList);*/

        // 进行业务逻辑的处理  - 进行获取当前帮手开设的科目数据


        return AjaxResult.success(obj);
    }


    @PostMapping("managerBsSubject")
    public AjaxResult managerBsSubject(@RequestBody String param){
        JSONObject obJson = JSONObject.parseObject(param);
        String userid = obJson.getString("userid"); // 获取用户id
        String subid = obJson.getString("subid");  //  获取科目id
        ByBs bsEntity = byBsService.queryBsEntityByWxid(userid);
        if(bsEntity == null){ // 获取帮手信息 未获取到数据，不能进行设置版型
            return AjaxResult.error(400,"你还不是帮手，请先进行帮手注册！");
        }
        String bsid = bsEntity.getId().toString();
        byBsService.saveOrDelBsSubject(bsid,subid);
        return AjaxResult.success();
    }


}
